﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

using CE.CarInsurance;
using CE.Global;

public partial class Admin_car_insurance_AddNewCarModel : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session[CE.Global.General.UserId] == null)
        {
            Response.Redirect("~/Admin/AdminSecureLogin.aspx");
        }

        if (!Page.IsPostBack)
        {
            DataTable dtCarMake = General.GetComboItem("TH.CarMake", " [Description]", true);
            ddlCarMake.DataSource = dtCarMake;
            ddlCarMake.DataTextField = dtCarMake.Columns[0].ColumnName.ToString();
            ddlCarMake.DataValueField = dtCarMake.Columns[1].ColumnName.ToString();
            ddlCarMake.DataBind();
            ddlCarMake.Items.Insert(0, new ListItem("Select", "-1"));

            if (Request.Params["CarMakeId"] != null && Request.Params["CarModelId"] != null)
            {
                int _carmakeid;
                int _carmodelid;
                if (Int32.TryParse(Request.Params["CarMakeId"].ToString(), out _carmakeid))
                {
                    if (Int32.TryParse(Request.Params["CarModelId"].ToString(), out _carmodelid))
                    {
                        ShowCarModel(_carmakeid, _carmodelid);
                    }
                }
            }
        }
    }

    private void ShowCarModel(int CarMakeId, int CarModelId)
    {
        CarModel _carmodel = new CarModel();
        CarModelDB _carmodeldb = new CarModelDB();
        _carmodel = _carmodeldb.GetCarModel(CarMakeId, CarModelId);
        if (_carmodel != null)
        {
            lblCarMakeId.Text = _carmodel.CarMakeId.ToString();
            lblCarModelId.Text = _carmodel.CarModelId.ToString();
            ddlCarMake.Text = _carmodel.CarMakeId.ToString();
            txtCarModel.Text = _carmodel.CarModelId.ToString();
            txtCarModel.Enabled = false;
            txtDescription.Text = _carmodel.Description;
            txtCarCode.Text = _carmodel.CarCode;
            txtBody.Text = _carmodel.Body;
            txtCC.Text = _carmodel.CC;
            chkCarModelStatus.Checked = _carmodel.Status;
            btnSubmit.Text = "Update";
        }
    }

    protected void btnClearForm_Click(object sender, EventArgs e)
    {
        ClearForm();
    }

    private void ClearForm()
    {
        lblMessage.Text = "";
        lblCarMakeId.Text = "";
        lblCarModelId.Text = "";
        ddlCarMake.Text = "-1";
        txtCarModel.Text = "";
        txtDescription.Text = "";
        txtCarCode.Text = "";
        txtBody.Text = "";
        txtCC.Text = "";
        chkCarModelStatus.Checked = false;
        txtCarModel.Enabled = true;
        btnSubmit.Text = "Submit";
    }

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        //Allow to update Car Make ID
        //Not Allow to update Car Model ID after created

        CarModel _carmodel = new CarModel();
        CarModelDB _carmodeldb = new CarModelDB();
        int _carmakeId;
        int _carmodelId;
        int _carmakemodelid;

        bool _isexist = false;

        if (lblCarMakeId.Text.Length == 0 && lblCarModelId.Text.Length == 0)
        {
            _carmakeId = Convert.ToInt32(ddlCarMake.Text);
            _carmodelId = _carmodeldb.GetCarModelNextId(_carmakeId);

            if (_carmodelId > 0)
            {
                txtCarModel.Text = _carmodelId.ToString();
            }
            else
            {
                lblMessage.Text = "Car model Id is invalid or zero!";
                return;
            }
        }
        else
        {
            if (Int32.TryParse(lblCarMakeId.Text, out _carmakeId) == false)
            {
                lblMessage.Text = "Can't update! Car Make Id can't parse.";
            }

            if (Int32.TryParse(lblCarModelId.Text, out _carmodelId) == false)
            {
                lblMessage.Text = "Can't update! Car Model Id can't parse.";
            }
        }

        if (!Page.IsValid)
        {
            lblMessage.Text = "Page is not valid!";
            return;
        }

        if (_carmakeId != 0 && _carmodelId != 0)
        {
            _isexist = _carmodeldb.CheckCarModelId(_carmakeId, _carmodelId, out _carmakemodelid);

            if (_isexist)
            {
                if (lblCarMakeId.Text.Length == 0 && lblCarModelId.Text.Length == 0)
                {
                    lblMessage.Text = "Car Model Id already exists!";
                    txtCarModel.Focus();
                    return;
                }
                else
                {
                    _carmodel.MakeModelId = _carmakemodelid;
                }
            }
            _carmodel.CarMakeId = _carmakeId;
            _carmodel.CarModelId = _carmodelId;
            _carmodel.CarCode = txtCarCode.Text;
            _carmodel.Description = txtDescription.Text;
            _carmodel.Body = txtBody.Text;
            _carmodel.CC = txtCC.Text;
            _carmodel.Status = chkCarModelStatus.Checked;

            _carmodel.MakeModelId = _carmodeldb.Insert(_carmodel);

            if (_carmodel.MakeModelId > 0)
            {
                if (btnSubmit.Text.ToUpper() == "UPDATE")
                {
                    lblMessage.Text = "Update successfully!";
                }
                else
                {
                    lblMessage.Text = "Insert successfully!";
                }
            }
            else
            {
                if (btnSubmit.Text.ToUpper() == "UPDATE")
                {
                    lblMessage.Text = "Update failed!";
                }
                else
                {
                    lblMessage.Text = "Insert failed!";
                }
            }
        }
    }
    protected void ddlCarMake_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
}
